package com.grablist.feihu.grablist.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

/**
 * 2017 2 月22号
 *
 * @author chenjian 设置安卓控件基本属性工具类
 */
public class CommonViewUtil {

    /**
     * view 设置背景
     *
     * @param view
     * @param id
     */
    public static void setBackground(View view, int id) {
        view.setBackgroundResource(id);
    }

    /**
     * 设置textview 背景 id 默认为-1 不显示对应图片
     *
     * @param view
     * @param context
     * @param
     * @param
     * @param
     * @param
     * @param leftId
     * @param topId
     * @param rightId
     * @param belowId
     */
    public static void setDarw(TextView view, Context context, int leftId,
                               int topId, int rightId, int belowId) {
        Drawable left = null;
        Drawable top = null;
        Drawable right = null;
        Drawable below = null;
        if (leftId != -1) {
            left = context.getResources().getDrawable(leftId);
            left.setBounds(0, 0, left.getMinimumWidth(),
                    left.getMinimumHeight());
        }
        if (topId != -1) {
            top = context.getResources().getDrawable(topId);
            top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());
        }
        if (rightId != -1) {
            right = context.getResources().getDrawable(rightId);
            right.setBounds(0, 0, right.getMinimumWidth(),
                    right.getMinimumHeight());
        }
        if (belowId != -1) {
            below = context.getResources().getDrawable(belowId);
            below.setBounds(0, 0, below.getMinimumWidth(),
                    below.getMinimumHeight());
        }
        view.setCompoundDrawables(left, top, right, below);
    }

    /**
     * 设置textView 文字显示不同颜色可传RGB 可以穿16进制
     *
     * @param textView
     * @param info
     * @param startIndex
     * @param endIndex
     * @param color
     * @param red
     * @param green
     * @param blue
     */

    public static void setTextColor(TextView textView, String info,
                                    int startIndex, int endIndex, String color, int red, int green,
                                    int blue) {
        if (color != null) {
            int windowPos[] = setHexToRGB(color);
            if (endIndex > info.length() || startIndex < 0) {
                return;
            }
            SpannableStringBuilder style = new SpannableStringBuilder(info);
            style.setSpan(
                    new ForegroundColorSpan(Color.rgb(windowPos[0],
                            windowPos[1], windowPos[2])), startIndex, endIndex,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(style);
        } else if ((0 <= red && red <= 255) && (0 <= green && green <= 255)
                && (0 <= blue && blue <= 255)) {
            if (endIndex > info.length() || startIndex < 0) {
                return;
            }
            SpannableStringBuilder style = new SpannableStringBuilder(info);
            style.setSpan(new ForegroundColorSpan(Color.rgb(red, green, blue)),
                    startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(style);
        }

    }

    /**
     * 设置textView 文字显示不同颜色可传RGB 可以穿16进制
     *
     * @param textView
     * @param info
     * @param startIndex
     * @param endIndex
     * @param color
     * @param
     * @param
     * @param
     */

    public static void setTextBackSingleColor(TextView textView, String info,
                                              int startIndex, int endIndex, String color, String textcolor) {
        if (color != null) {
            int windowPos[] = setHexToRGB(color);
            if (endIndex > info.length() || startIndex < 0) {
                return;
            }
            int windowPos1[] = setHexToRGB(textcolor);
            if (endIndex > info.length() || startIndex < 0) {
                return;
            }
            if (windowPos1 == null || windowPos == null) {
                return;
            }
            SpannableStringBuilder style = new SpannableStringBuilder(info);
            style.setSpan(
                    new BackgroundColorSpan(Color.rgb(windowPos[0],
                            windowPos[1], windowPos[2])), startIndex, endIndex,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            style.setSpan(
                    new ForegroundColorSpan(Color.rgb(windowPos1[0],
                            windowPos1[1], windowPos1[2])), startIndex,
                    endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(style);
        }
    }

    /**
     * 实现16进制转RGB 颜色
     *
     * @param hex
     */

    public static int[] setHexToRGB(String hex) {
        final int windowPos[] = new int[3];
        if (!(hex.length() == 7)) {
            return null;
        }

        String str1 = hex.substring(1, 3);
        String str2 = hex.substring(3, 5);
        String str3 = hex.substring(5, 7);
        windowPos[0] = Integer.parseInt(str1, 16);
        windowPos[1] = Integer.parseInt(str2, 16);
        windowPos[2] = Integer.parseInt(str3, 16);
        return windowPos;
    }

    /**
     * 实先转RGB转16进制颜色 颜色
     *
     * @param
     */

    public static String setHexToRGB(int red, int green, int blue) {

        if ((0 <= red && red <= 255) && (0 <= green && green <= 255)
                && (0 <= blue && blue <= 255)) {
            String color = new String("#");
            color = color + Integer.toHexString(red)
                    + Integer.toHexString(green) + Integer.toHexString(blue);
            return color;
        } else {
            return null;
        }


    }

    /**
     * 简单设置文字颜色
     *
     * @param textView
     * @param context
     * @param id
     */
    public static void setTextSingle(TextView textView, Context context, int id) {
        if (id != -1) {
            Resources resource = (Resources) context.getResources();
            ColorStateList csl = (ColorStateList) resource
                    .getColorStateList(id);
            textView.setTextColor(csl);
        }

    }

    /**
     * 设置时钟显示选择时间
     *
     * @param width
     * @param hight
     * @param x
     * @param y
     * @return
     */

    public static int setHour(int width, int hight, int x, int y, int type) {

        int hour = 0;
        if (x == width && y <= hight) {
            hour = 0;
        }

        if (x == width && y > hight) {
            hour = 6;
        }
        if (x <= width && y == hight) {
            hour = 9;
        }
        if (x > width && y == hight) {
            hour = 3;
        }

        int X = x - width;
        int Y = y - hight;
        double z = (double) Y / X;

        if (x > width) {
            hour = (int) (((Math.toDegrees(Math.atan(z)) + 90) / 360) * type);
        }

        if (x < width) {
            hour = (int) (((Math.toDegrees(Math.atan(z)) + 270) / 360) * type);
        }

        return hour;
    }


}
